package com.hongguan.oms.web.AfterService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.hongguan.oms.service.AfterService.AfterService;
import com.hongguan.oms.utils.Util;
import com.hongguan.oms.web.BaseHandler;
//UTF-8
/**
 * 
 * @author tonylin <br>
 * @create 2011-1-8 <br>
 * @project Haojin <br>
 * @copyright 2011 HongGuan Information. All rights reserved
 * @desc <br>
 */

public class AfterServiceHead extends BaseHandler {

	private AfterService afterService;

	public void setAfterService(AfterService afterService) {
		this.afterService = afterService;
	}

	public ModelAndView updateAfterOrder(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		
		ModelAndView mv =  new ModelAndView("/updateAfterOrder");
		Date date = new Date();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Map<String, Object> personmap = new HashMap<String, Object>();
			
			
			String afterServiceId = request.getParameter("ids");
			String servicePerson = request.getParameter("servicePerson");
			String[] afterServiceIdArray = afterServiceId.split(",");
			String[] servicePersonArray = servicePerson.split(",");
			
			for (int i = 0; i < afterServiceIdArray.length; i++) {
				map.put("servicePerson", servicePerson);
				map.put("afterServiceId", afterServiceIdArray[i]);


				map.put("user_Id", Util.getUserIdFromSession(request));
				map.put("OrderByTime", new SimpleDateFormat("yyyy-MM-dd")
						.format(date));
				afterService.updateAfterOrder(map);
				
				for (int j=0;j<servicePersonArray.length;j++){
					
					personmap.put("AfterServiceID", afterServiceIdArray[i]);
					personmap.put("ServicePerson", servicePersonArray[j]);
					afterService.addAfterOrderByperson(personmap);
				}
				
			}
			mv.addObject(SUCCESS, "true");


		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject(ERROR, "false");

		}
		return mv;
	}

	public ModelAndView searchAfterOrder(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {

		ModelAndView mv = null;
		mv = this.getSuccessModelView();
		Map<String, Object> map = new HashMap<String, Object>();
		try {

			if (request.getParameter("searchComplateDate").equals("")) {
			} else {
				map.put("searchComplateDate", request
						.getParameter("searchComplateDate"));
			}

			if (request.getParameter("searchEndDate").equals("")) {
			} else {
				map.put("searchEndDate", request.getParameter("searchEndDate"));
			}

			if ("".equals(request.getParameter("searchName"))) {

			} else {
				map.put("searchName", request.getParameter("searchName"));
			}

			if ("".equals(request.getParameter("searchArea"))) {

			} else {
				map.put("searchArea", request.getParameter("searchArea"));
			}

			if ("".equals(request.getParameter("searchAfterServiceId"))) {

			} else {
				map.put("searchAfterServiceId", request
						.getParameter("searchAfterServiceId"));
			}

			mv.addObject("result", afterService.searchAfterOrder(map));

			mv.addObject(SUCCESS, "true");

		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject(ERROR, "false");
		}
		return mv;

	}

	public ModelAndView addAfterOrder(HttpServletRequest request,
			HttpServletResponse respnose) throws Exception {
		ModelAndView mv = null;
		mv = this.getSuccessModelView();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Date date = new Date();

			if (request.getParameter("AFcustomerName") != null) {
				map.put("AFcustomerName", request
						.getParameter("AFcustomerName"));
			}

			if (request.getParameter("AFserviceAddress") != null) {
				map.put("AFserviceAddress", request
						.getParameter("AFserviceAddress"));
			}

			if (request.getParameter("AFcustomerArea") != null) {
				map.put("AFcustomerArea", request
						.getParameter("AFcustomerArea"));
			}
			if (request.getParameter("AFcustomerID") != null) {
				map.put("AFcustomerID", request.getParameter("AFcustomerID"));
			}
			if (request.getParameter("reason") != null) {
				map.put("reason", request.getParameter("reason"));
			}

			if (request.getParameter("AFserviceStartDate").equals("")) {
			} else {
				map.put("AFserviceStartDate", request
						.getParameter("AFserviceStartDate"));
			}
			if (request.getParameter("AFconnName") != null) {
				map.put("AFconnName", request.getParameter("AFconnName"));
			}
			if (request.getParameter("AFconnOffTele") != null) {
				map.put("AFconnOffTele", request.getParameter("AFconnOffTele"));
			}
			if (request.getParameter("AFconnMobile") != null) {
				map.put("AFconnMobile", request.getParameter("AFconnMobile"));
			}
			if (request.getParameter("AFconnEmail") != null) {
				map.put("AFconnEmail", request.getParameter("AFconnEmail"));
			}
			if (request.getParameter("reason") != null) {
				map.put("reason", request.getParameter("reason"));
			}

			map.put("CreateBy", Util.getUserIdFromSession(request));
			map.put("CreateTime", new SimpleDateFormat("yyyy-MM-dd")
					.format(date));

			afterService.addAfterOrder(map);

			mv.addObject(SUCCESS, "true");
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject(ERROR, "false");
		}
		return mv;
	}
}
